package com.zstu.lvxiang.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zstu.lvxiang.mapper.RecordMapper;
import com.zstu.lvxiang.pojo.Records;
import com.zstu.lvxiang.service.IRecordService;
import com.zstu.lvxiang.utils.DateUtils;
import com.zstu.lvxiang.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Trafalgar
 * @since 2022-03-16
 */
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Records> implements IRecordService {
    @Autowired
    RecordMapper recordMapper;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 得到每天的时间分配情况
     *
     * @param sno
     * @param date
     * @return
     */
    @Override
    public Map<String, Float> dailyInfo(String sno, String date) {
        Map result = (Map) redisTemplate.opsForValue().get("report_sys_" + sno + date);
        if (result != null) {
            return result;
        }
        HashMap<String, Float> map = new HashMap<>();
        Float sum = Float.valueOf(0);
        List<ResultVo> records = recordMapper.selectList(sno, date);
        for (ResultVo r : records) {
            map.put(r.getName(), r.getTime());
            sum += r.getTime();
        }
        map.put("总学习时长", sum);
        redisTemplate.opsForValue().set("report_sys_" + sno + date, map);
        return map;
    }

    /**
     * 得到班级排名
     *
     * @param sno
     * @param date
     * @param classroom
     * @return
     */
    @Override
    public Integer getRowName(String sno, String date, String classroom) {
        Integer result = (Integer) redisTemplate.opsForValue().get("report_sys_" + sno + date + "rank");
        if (result != null) {
            return result;
        }
        Integer rank = recordMapper.getRowNum(sno, date, classroom);
        redisTemplate.opsForValue().set("report_sys_" + sno + date + "rank", rank);
        return rank;
    }

    /**
     * 得到班级的平均学习时长
     *
     * @param date
     * @param classroom
     * @return
     */
    @Override
    public Float getClassStudy(String date, String classroom) {
        return recordMapper.getClassStudy(date, classroom);
    }

}
